class Solution {
    public:
        int maxProduct(vector<int>& nums) {
            int n = nums.size();
            vector<int> f(n);
            vector<int> g(n);
            f[0]=g[0] = nums[0];
            int ret = nums[0];
            for(int i = 1 ; i < n ; i++){
               if(nums[i] > 0){
                f[i] = max(f[i-1]*nums[i] , nums[i]);
                g[i] = min(g[i-1]*nums[i] , nums[i]);
               }else{
                f[i] = max(g[i-1]*nums[i] , nums[i]);
                g[i] = min(f[i-1]*nums[i] , nums[i]);
               }
                ret = max(ret,f[i]);
            }
            return ret;
        }
    };